/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class Settings {

    private static Settings instance;

    public static Settings loadSettings() {
        if (instance != null) {
            return instance;
        } else {
            try {
                XMLDecoder decoder = new XMLDecoder(new FileInputStream("livetracker.settings"));
                instance = (Settings) decoder.readObject();
                decoder.close();
            } catch (FileNotFoundException ex) {
                logger.log(Level.INFO, "Settings file not found", ex);
                instance = new Settings();
            }
            return instance;
        }
    }

    public static class Server {

        private String hostname;
        private int port;

        public Server() {
        }

        public Server(String hostname, int port) {
            this.hostname = hostname;
            this.port = port;
        }

        public String getHostname() {
            return hostname;
        }

        public void setHostname(String hostname) {
            this.hostname = hostname;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        @Override
        public String toString() {
            return hostname + ":" + port;
        }
    }
    private static final Logger logger = Logger.getLogger(Settings.class.getName());
    private List<Server> servers = new ArrayList<Settings.Server>();

    public Settings() {
    }

    public List<Server> getServers() {
        return servers;
    }

    public void setServers(List<Server> servers) {
        this.servers = servers;
    }

    public void save() {
        try {
            XMLEncoder encoder = new XMLEncoder(new FileOutputStream("livetracker.settings"));
            encoder.writeObject(this);
            encoder.close();
        } catch (FileNotFoundException ex) {
            logger.log(Level.INFO, "Settings file not found", ex);
        }
    }
}
